package com.etime.controller;

import com.etime.pojo.Station;
import com.etime.pojo.Ticket;
import com.etime.service.StationService;
import com.etime.service.TicketService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;


@Controller
@RequestMapping("ticket")
public class TicketController {
    @Autowired
    private TicketService ticketService;
    @Autowired
    private StationService stationService;

    @RequestMapping("getTicketByPage")
    public ModelAndView getTicketByPage(Integer page,ModelMap modelMap){
        PageInfo<Ticket> pageInfo=ticketService.getTicketByPage(page);
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.setViewName("back/ticket");
        modelMap.addAttribute("data",pageInfo);
        return modelAndView;
    }
    @RequestMapping("getTicketByNo")
    public ModelAndView getTicketByNo(Integer page,String train_no,ModelMap modelMap){
        PageInfo<Ticket> pageInfo=ticketService.getTicketByNo(page,train_no);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("back/ticket");
        modelMap.addAttribute("data",pageInfo);
        modelMap.addAttribute("train_no",train_no);
        return modelAndView;
    }
    @RequestMapping("addTicket")
    public String addTicket(Ticket ticket){
        boolean result=ticketService.addTicket(ticket);
        if(result){
            return "redirect:getTicketByPage?page=1";
        }else{
            return "redirect:../station/getAllStation";
        }
    }

    @RequestMapping("getTicketById")
    public ModelAndView getTicketById(Integer train_id,ModelMap modelMap){
        Ticket ticket = ticketService.getTicketById(train_id);
        List<Station> list=stationService.getAllStation();
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.setViewName("back/editticket");
        modelMap.addAttribute("ticket",ticket);
        modelMap.addAttribute("stationList",list);
        return modelAndView;
    }
    @RequestMapping("editTicket")
    public String editTicket(Ticket ticket){
        boolean result=ticketService.editTicket(ticket);
        if(result){
            return "redirect:getTicketByPage?page=1";
        }else{
            return "redirect:getTicketById?train_id="+ticket.getTrain_id();
        }
    }

    @RequestMapping("getTicketBySth")
    public ModelAndView getTicketBySth(Ticket ticket,ModelMap modelMap){
        List<Ticket> list = ticketService.getTicketBySth(ticket);
        Station departure = stationService.getStationById(ticket.getDeparture_station());
        Station terminal = stationService.getStationById(ticket.getTerminal_station());
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.setViewName("forward/showtrains");
        modelMap.addAttribute("ticketList",list);
        modelMap.addAttribute("departure",departure);
        modelMap.addAttribute("terminal",terminal);
        modelMap.addAttribute("train_date",ticket.getTrain_date());
        return modelAndView;
    }

    @RequestMapping("getTicketByIdForBuy")
    public ModelAndView getTicketByIdForBuy(Integer train_id,String type,ModelMap modelMap){
        Ticket ticket=ticketService.getTicketById(train_id);
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.setViewName("forward/buy");
        modelMap.addAttribute("ticket",ticket);
        modelMap.addAttribute("type",type);
        return modelAndView;
    }











}
